//
//  PBCollectViewController.m
//  PBHomeDemo
//
//  Created by Playboy on 2017/11/21.
//  Copyright © 2017年 tmkj. All rights reserved.
//

#import "PBCollectViewController.h"
#import "PBCollectionHomeCell.h"
#import "PBBestHomeModel.h"
#import "PBHomeDetailController.h"
#import "PBHomeCell.h"
#import "TMEmptyView.h"
#import "TMNotLogInView.h"

static NSString * const kPBCollectionHomeCell = @"PBCollectionHomeCell";
static NSString * const kPBHomeCell = @"PBHomeCell";

@interface PBCollectViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray    *dataSource;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) TMEmptyView *emptyView;
@property (nonatomic, strong) TMNotLogInView *notLoginView;
@end

@implementation PBCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self configure];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self configureData];
}
- (void)configure {
    self.page = 1;
    [self configureView];
}
- (void)configureView {
    [self.tableView registerNib:[UINib nibWithNibName:kPBCollectionHomeCell bundle:nil] forCellReuseIdentifier:kPBCollectionHomeCell];
    [self.tableView registerNib:[UINib nibWithNibName:kPBHomeCell bundle:nil] forCellReuseIdentifier:kPBHomeCell];
    // 刷新
    [TMRefreshHelper headerRefresh:self.tableView withTarget:self action:@selector(headerRefresh)];
    [TMRefreshHelper footerRefresh:self.tableView withTarget:self action:@selector(footerRefresh)];
}
- (void)configureData {
    NSDictionary *param = @{@"pageSize":@"10",@"currentPage":[NSString stringWithFormat:@"%zd",self.page]};
    [TMNetwork requestGetWithParam:param url:@"/house/collection/list" callback:^(TMBaseReqModel *model) {
        [self.tableView.mj_header endRefreshing];
        if (model.code == 200) {
            if (self.notLoginView) {
                [self.notLoginView removeFromSuperview];
            }
            self.dataSource = [PBBestHomeModel objectArrayWithKeyValuesArray:model.data];
            if (self.dataSource.count < 10) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [self.tableView.mj_footer endRefreshing];
            }
            if (self.dataSource.count == 0) {
                [self.view addSubview:self.emptyView];
            }else {
                [self.emptyView removeFromSuperview];
            }
            [self.tableView reloadData];
        }else if (model.code == 401) {
            self.notLoginView.jumpVC = self;
            self.notLoginView.frame = self.view.frame;
            [self.view addSubview:self.notLoginView];
        }else {
            [self showHint:model.message];
        }
    }];
}

- (void)headerRefresh {
    self.page = 1;
    [self configureData];
}
- (void)footerRefresh {
    self.page += 1;
    NSDictionary *param = @{@"pageSize":@"10",@"currentPage":[NSString stringWithFormat:@"%zd",self.page]};
    [TMNetwork requestGetWithParam:param url:@"/house/collection/list" callback:^(TMBaseReqModel *model) {
        [self.tableView.mj_footer endRefreshing];
        if (model.code == 200) {
            NSArray *array = [PBBestHomeModel objectArrayWithKeyValuesArray:model.data];
            if (array.count < 10) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [self.tableView.mj_footer endRefreshing];
            }
            [self.dataSource addObjectsFromArray:array];
            [self.tableView reloadData];
        }else {
            [self showHint:model.message];
        }
    }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PBHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:kPBHomeCell forIndexPath:indexPath];
    [cell refreshWithModel:self.dataSource[indexPath.row]];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PBBestHomeModel *model = self.dataSource[indexPath.row];
    PBHomeDetailController *vc = [[PBHomeDetailController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.houseId = [NSString stringWithFormat:@"%zd",model.ID];
    [self.navigationController pushViewController:vc animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

#pragma mark - super
- (void)resetNavigationBarItems {
    self.title = @"收藏";
}
- (TMEmptyView *)emptyView {
    if (!_emptyView) {
        _emptyView = [TMEmptyView TMEmptyView];
        _emptyView.frame = self.view.frame;
    }
    return _emptyView;
}
- (TMNotLogInView *)notLoginView {
    if (!_notLoginView) {
        _notLoginView = [TMNotLogInView TMNotLogInView];
    }
    return _notLoginView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
